package com.learn.project2.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * \* Author: Valley
 * \* Date: 2022/1/8
 * \* Time: 11:50
 * \* Description:
 */

public class StringUtils {

    public static List<Integer> stringToInt(String s1){
        String[] split = s1.split("\\[")[1].split("\\]")[0].split(",");
        List<Integer> integers = new ArrayList<>();
        for (String s : split) {
            integers.add(Integer.parseInt(s));
        }
        return integers;
    }
    public static List<String> stringToStringArray(String s2){
        List<String> strings = new ArrayList<>();
        if ("[]".equals(s2)){
            return strings;
        }
        String[] split = s2.split("\\[")[1].split("\\]")[0].split(",");

        for (String s : split) {
            strings.add(s);
        }
        return strings;
    }

    public static Date stringToDate(String s){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String arrayToString(List<String> list){
        StringBuffer sb = new StringBuffer();
        for (String s : list) {
            sb.append(s);
        }
        return sb.toString();
    }

    public static Date dateToDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        Date newDate = stringToDate(format);
        return newDate;
    }

    public static String removeArray(String s){
        String[] split = s.split("\\[");
        String[] split1 = split[1].split("\\]");
        return split1[0];
    }

    public static String dateToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}
